package slumpen;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;

import tools.blast.blastM8Alignment;

public class OrfStat {

	public static void main(String[] args) throws Exception{
//		makeBlastFiles();
//		compareBlast(args[0]);
		linerizedCheck(args[0]);
	}
	
	public static void makeBlastFiles()throws Exception{
		String prefix="/opt/shared/ncRNA/blastOrf/";
		BufferedReader in= new BufferedReader(new FileReader("/opt/shared/ncRNA/genomeOrfStat.csv"));
		HashMap<String, BufferedWriter> outs= new HashMap<String, BufferedWriter>();
		String[] l;
		int start, end,nr=0;
		for(String s= in.readLine();s!=null;s=in.readLine(),nr++){
			l=s.split("\t");
			start=Integer.parseInt(l[2]);
			end=Integer.parseInt(l[3]);
			if(start<end){
				//sense
				if(!outs.containsKey(l[0]+"+")){
					outs.put(l[0]+"+", new BufferedWriter(new FileWriter(prefix+l[0]+"_sense.csv")));
				}
				outs.get(l[0]+"+").write(l[2]+"\t"+l[3]+"\t"+l[1]+"\t"+l[0]+"\n");
				
			}else{
				//antisense
				if(!outs.containsKey(l[0]+"-")){
					outs.put(l[0]+"-", new BufferedWriter(new FileWriter(prefix+l[0]+"_antisense.csv")));
				}
				outs.get(l[0]+"-").write(l[3]+"\t"+l[2]+"\t"+l[1]+"\t"+l[0]+"\n");
				
			}
		}
		for (BufferedWriter out : outs.values()) {
			out.close();
		}
	}
	public static void linerizedCheck(String file)throws Exception{
		BufferedReader in= new BufferedReader(new FileReader(file));
		OrfStat_Orf oo;
		OrfStat_cluster oc = new OrfStat_cluster();
		int count=0,cluster=0;
		for(String s=in.readLine();s!=null;s=in.readLine(),count++){
			if(count%10000==0){
				System.err.println(count+" ORFs and "+cluster+" clusters");
			}
			oo=new OrfStat_Orf(s);
			if(oc.overlap(oo)){
				oc.addData(oo);
			}else{
				//print old cluster and start new
				cluster++;
				oc.print(System.out);
				oc= new OrfStat_cluster(oo);
			}
		}
		oc.print(System.out);
	}
	
	public static void compareBlast(String file)throws Exception{
		BufferedReader in= new BufferedReader(new FileReader(file));
		int clusterNr=0;
		HashMap<Integer, OrfStat_cluster>clusters = new HashMap<Integer, OrfStat_cluster>();
		ArrayList<Integer> overlapping;
		OrfStat_Orf oo;
		int count=0;
		for(String s=in.readLine();s!=null;s=in.readLine(),count++){
			if(count%1000==0){
				System.err.println(count+" ORFs and "+clusters.size()+" clusters");
			}
			oo=new OrfStat_Orf(s);
			overlapping= new ArrayList<Integer>();
			for (Integer c : clusters.keySet()) {
				if(clusters.get(c).overlap(oo)){
					overlapping.add(c);
				}
			}
			if(overlapping.size()==0){
				//add new cluster
				clusters.put(clusterNr++, new OrfStat_cluster(oo));
			}else{
				//add orf to cluster
				clusters.get(overlapping.get(0)).addData(oo);
				//merge if more overlapping clusters present
				for(int i=1;i<overlapping.size();i++){
					clusters.get(overlapping.get(0)).addData(clusters.remove(overlapping.get(i)));
				}
			}
		}
		//print
//		System.out.println(clusters.size());
		for (OrfStat_cluster c : clusters.values()) {
			c.print(System.out);
		}
	}
	
}
